﻿// <copyright file="LeavedWordsDao.cs" company="NTalent">
// Copyright (c) 2010, NTalent, Inc. All rights reserved
// </copyright>
// <author>卢俊坤</author>
// <author>王彦博 符茂松</author>

namespace Net.Ntalent.Action50.Core.Dao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Dynamic;
    using Net.Ntalent.Action50.DataAccess;

    /// <summary>
    /// 对于T_LEAVED_WORDS实体的数据库操作实体
    /// </summary>
    /// <author>王彦博</author>
    /// <copyright>
    /// Copyright (c) 2010, NTalent, Co. Ltd. All rights reserved.
    /// </copyright>
    /// <version>1.0.0</version>  
    public class LeavedWrodsDao : IDisposable
    {
        /// <summary>
        /// 数据库操作实体类，readonly, 在构造函数初始化，无getter/setter
        /// </summary>
        private Action50DataContext context;

        /// <summary>
        /// 构造方法
        /// </summary>
        public LeavedWrodsDao()
        {
            this.context = new Action50DataContext();
        }

        /// <summary>
        /// 将传入的T_LEAVED_WORDS实体进行持久化.
        /// </summary>
        /// <param name="action">需要新建的T_LEAVED_WORDS实体</param>
        /// <returns>已持久化的T_LEAVED_WORDS实体</returns>
        public T_LEAVED_WORDS Create(T_LEAVED_WORDS words)
        {
            words.POST_DATETIME = DateTime.Now;
            this.context.T_LEAVED_WORDS.InsertOnSubmit(words);
            this.context.SubmitChanges();
            return words;
        }

        /// <summary>
        /// 根据给出的id获取T_LEAVED_WORDS实体.
        /// </summary>
        /// <remarks>
        /// 如获取失败，返回null
        /// </remarks>
        /// <param name="id">需要获取的T_LEAVED_WORDS实体的id</param>
        /// <returns>符合条件的T_LEAVED_WORDS实体</returns>
        public T_LEAVED_WORDS Retrieve(long id)
        {
            return this.context.T_LEAVED_WORDS.Count(w => w.Id == id) == 1 ?
                this.context.T_LEAVED_WORDS.Single(w => w.Id == id) : null;
        }

        /// <summary>
        /// 获取所有T_LEAVED_WORDS，返回结果集。
        /// </summary>
        /// <remarks>
        /// 若无任何结果，则返回空集合.
        /// </remarks>
        /// <param name="pageNumber">
        /// 需要获取的信息的页码,如果是0，则获取全部.
        /// </param>
        /// <param name="pageSize">
        /// 获取的页面大小,在pageNumber为0时此值将被忽略(可以为任意值)
        /// </param>
        /// <param name="orderBy">
        /// 排序表达式。获取的结果集将按此表达式排序。如果是empty，则按照id升序排列。
        /// </param>
        /// <returns>页面内所有的T_LEAVED_WORDS实体</returns>
        public IList<T_LEAVED_WORDS> RetrieveAll(int pageNumber, int pageSize, string orderBy)
        {
            //若orderBy为empty,按照id升序排列
            orderBy = String.IsNullOrEmpty(orderBy) ? "Id ascending" : orderBy;
            IQueryable<T_LEAVED_WORDS> results = null;

            //根据页码查找实体
            if (pageNumber == 0)
            {
                results = this.context.T_LEAVED_WORDS.OrderBy(orderBy);
            }
            else if (pageNumber > 0 && pageSize > 0)
            {
                results = this.context.T_LEAVED_WORDS.OrderBy(orderBy)
                    .Skip((pageNumber - 1) * pageSize).Take(pageSize);
            }

            //若无结果，返回空集合
            return results == null || results.Count() == 0 ? new List<T_LEAVED_WORDS>() : results.ToList();
        }

        /// <summary>
        /// 根据news或者logs的id来获取所有T_LEAVED_WORDS
        /// </summary>
        /// <remarks>
        /// 若无结果，则返回空集合。 
        /// </remarks>
        /// <param name="logId">
        /// news或者logs的id
        /// </param>
        /// <param name="pageNumber">
        /// 需要获取的信息的页码,如果是0，则获取全部.
        /// </param>
        /// <param name="pageSize">
        /// 获取的页面大小,在pageNumber为0时此值将被忽略(可以为任意值)
        /// </param>
        /// <param name="orderBy">
        /// 排序表达式。获取的结果集将按此表达式排序。如果是empty，则按照id升序排列。
        /// </param>
        /// <returns>页面内NEWS_AND_LOGS_ID等于id的所有T_LEAVED_WORDS实体的集合</returns>
        public IList<T_LEAVED_WORDS> RetrieveByLog(long logId, int pageNumber, int pageSize, string orderBy)
        {
            //若orderBy为empty,按照id升序排列
            orderBy = String.IsNullOrEmpty(orderBy) ? "Id ascending" : orderBy;
            IQueryable<T_LEAVED_WORDS> results = null;

            //根据页码和NEWS_AND_LOGS_ID查找实体
            if (pageNumber == 0)
            {
                results = this.context.T_LEAVED_WORDS.Where(w => w.NEWS_AND_LOGS_ID == logId).OrderBy(orderBy);
            }
            else if (pageNumber > 0 && pageSize > 0)
            {
                results = this.context.T_LEAVED_WORDS
                    .Where(w => w.NEWS_AND_LOGS_ID == logId).OrderBy(orderBy)
                    .Skip((pageNumber - 1) * pageSize).Take(pageSize);
            }

            //若无结果，返回空集合
            return results == null || results.Count() == 0 ? new List<T_LEAVED_WORDS>() : results.ToList();
        }

        /// <summary>
        /// 根据传入的T_LEAVED_WORDS的id来删除实体
        /// </summary>
        /// <remarks>
        /// 忽略所有exception
        /// </remarks>
        /// <param name="id">欲删除的T_LEAVED_WORDS的ID</param>
        public void Delete(long id)
        {
            try
            {
                var entities = this.context.T_LEAVED_WORDS.Where(w => w.Id == id);
                this.context.T_LEAVED_WORDS.DeleteAllOnSubmit(entities);
                this.context.SubmitChanges();
            }
            catch
            {
                // 忽略所有异常
            }
        }

        /// <summary>
        /// 将传入的T_LEAVED_WORDS实体信息更新
        /// </summary>
        /// <remarks>
        /// 更新失败则返回null
        /// </remarks>
        /// <param name="words">
        /// 欲更新的实体
        /// </param>
        /// <returns>
        /// 已更新的实体
        /// </returns>
        public T_LEAVED_WORDS Update(T_LEAVED_WORDS words)
        {
            if (this.context.T_LEAVED_WORDS.Count(w => w.Id == words.Id) == 1)
            {
                // 1 获取数据库中的实体
                var entity = this.context.T_LEAVED_WORDS.Single(w => w.Id == words.Id);

                // 2 将传入的logs实体中的所有字段赋值到entity

                entity.ACTION_ID = words.ACTION_ID;
                entity.NEWS_AND_LOGS_ID = words.NEWS_AND_LOGS_ID;
                entity.POST_DATETIME = words.POST_DATETIME;
                entity.USER_EMAIL = words.USER_EMAIL;
                entity.USER_IP = words.USER_IP;
                entity.WORDS_CONTENT = words.WORDS_CONTENT;
                // 3 执行更新
                this.context.SubmitChanges();

                // 4 返回更新后的实体
                return entity;
            }
            //更新失败返回null
            return null;
        }

        //暂不执行任何操作
        public void Dispose()
        {
        }
    }
}
